import request from '@/utils/http'

// 与后端 MenuCreate / MenuUpdate 对齐的请求参数
export interface MenuCreate {
  name: string
  code: string
  desc?: string | null
  parent_id?: number | null
  type: number // 1 菜单，2 按钮
  path?: string | null
  component?: string | null
  icon?: string | null
  sort?: number | null
  status?: boolean | null
  is_hidden?: boolean | null
  keep_alive?: boolean | null
  is_iframe?: boolean | null
  link?: string | null
}

export interface MenuUpdate {
  name?: string
  code?: string
  desc?: string | null
  parent_id?: number | null
  type?: number // 1 菜单，2 按钮
  path?: string | null
  component?: string | null
  icon?: string | null
  sort?: number | null
  status?: boolean | null
  is_hidden?: boolean | null
  keep_alive?: boolean | null
  is_iframe?: boolean | null
  link?: string | null
}

// 后端返回的菜单/权限响应，最重要的是 id
export interface MenuResponse {
  id: number
  name: string
  code: string
  type: number
  parent_id?: number | null
  path?: string | null
  component?: string | null
  icon?: string | null
  sort?: number | null
  status?: boolean | null
  is_hidden?: boolean | null
  keep_alive?: boolean | null
  is_iframe?: boolean | null
  link?: string | null
}

/** 创建菜单/权限 */
export function createMenu(params: MenuCreate) {
  return request.post<MenuResponse>({
    url: '/system/menus/create',
    params,
    showSuccessMessage: true
  })
}

/** 更新菜单/权限 */
export function updateMenu(permissionId: number, params: MenuUpdate) {
  return request.put<MenuResponse>({
    url: `/system/menus/${permissionId}`,
    params,
    showSuccessMessage: true
  })
}

/** 删除菜单/权限 */
export function deleteMenu(permissionId: number) {
  return request.del<{ message: string }>({
    url: `/system/menus/${permissionId}`,
    showSuccessMessage: true
  })
}